﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows;
using UtilZ.Dotnet.WindowsDesktopEx.WPF;

namespace UtilZ.Dotnet.WindowsDesktopEx.WPF
{
    /// <summary>
    /// 数值与Bool转换接口(大于0Visibility.Visible,小于0Visibility.Collapsed)
    /// </summary>
    public class NumberToVisibilityConverter : ValueConverterAbs
    {
        /// <summary>
        /// 构造函数 
        /// </summary>
        public NumberToVisibilityConverter()
        {

        }

        /// <summary>
        /// 将后台对象转换为显示的对象
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return Visibility.Collapsed;
            }

            Visibility visibility = Visibility.Collapsed;
            TypeCode typeCode = Type.GetTypeCode(value.GetType());
            switch (typeCode)
            {
                case TypeCode.Boolean:
                    if ((bool)value)
                    {
                        visibility = Visibility.Visible;
                    }
                    break;
                case TypeCode.DateTime:
                    visibility = Visibility.Visible;
                    break;
                case TypeCode.Decimal:
                    if ((decimal)value > decimal.Zero)
                    {
                        visibility = Visibility.Visible;
                    }
                    break;
                case TypeCode.Double:
                    if ((double)value > double.Epsilon)
                    {
                        visibility = Visibility.Visible;
                    }
                    break;
                case TypeCode.Char:
                    if ((UInt16)value > 0)
                    {
                        visibility = Visibility.Visible;
                    }
                    break;
                case TypeCode.Byte:
                    if ((byte)value > 0)
                    {
                        visibility = Visibility.Visible;
                    }
                    break;
                case TypeCode.Int16:
                    if ((Int16)value > 0)
                    {
                        visibility = Visibility.Visible;
                    }
                    break;
                case TypeCode.Int32:
                    if ((Int32)value > 0)
                    {
                        visibility = Visibility.Visible;
                    }
                    break;
                case TypeCode.Int64:
                    if ((Int64)value > 0)
                    {
                        visibility = Visibility.Visible;
                    }
                    break;
                case TypeCode.SByte:
                    if ((sbyte)value > 0)
                    {
                        visibility = Visibility.Visible;
                    }
                    break;
                case TypeCode.Single:
                    if ((float)value > float.Epsilon)
                    {
                        visibility = Visibility.Visible;
                    }
                    break;
                case TypeCode.UInt16:
                    if ((UInt16)value > 0)
                    {
                        visibility = Visibility.Visible;
                    }
                    break;
                case TypeCode.UInt32:
                    if ((UInt32)value > 0)
                    {
                        visibility = Visibility.Visible;
                    }
                    break;
                case TypeCode.UInt64:
                    if ((UInt64)value > 0)
                    {
                        visibility = Visibility.Visible;
                    }
                    break;
                case TypeCode.Object:
                case TypeCode.Empty:
                case TypeCode.DBNull:
                default:
                    break;
            }

            return visibility;
        }
    }
}
